package com.example.demo.test;

import com.alibaba.fastjson.JSON;
import com.google.protobuf.ByteString;
import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;
import com.volcengine.service.vod.model.request.VodSubmitDirectEditTaskAsyncRequest;
import com.volcengine.service.vod.model.response.VodSubmitDirectEditTaskAsyncResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SubmitDirectEditTaskAsyncDemo {
    public static void main(String[] args) {
        IVodService vodService = VodServiceImpl.getInstance();

        // call below method if you dont set ak and sk in ～/.vcloud/config
        vodService.setAccessKey("AKLTZmEyNzdjZDc0MGVkNDU4MGI5NDAyNjkzM2I0ZDVlNDU");
        vodService.setSecretKey("WVRnM01qRTFNRGxoWW1VM05ERmpOR0ZqWldOaVpESTVOR016TjJJell6Yw==");


        Map<String, Object> params = new HashMap<>();
        params.put("Upload", new HashMap<String, Object>(){{
            put("SpaceName", "hok");
            put("VideoName", "test.mp4"); //业务自定义
        }});
        params.put("Output",new HashMap<String,Object>(){
            {
                put("Alpha",false);
                put("Fps",24.99);
                put("DisableVideo",false);
                put("DisableAudio",false);
//                put("Cover",new HashMap<String,Object>(){
//                    {
//                        put("DisableCover",true);
//                    }
//                });
                put("Codec",new HashMap<String, Object>(){
                    {
                        put("VideoCodec","h264");
                        put("Preset","slow");
                        put("Crf",23);
                        put("AudioCodec","aac");
                        put("AudioBitrate",128);
                    }});
            }
        });

        params.put("Track", new ArrayList<Object>() {
            {
                add(new ArrayList<HashMap>() {
                    {
                        add(new HashMap<String, Object>() {
                            {
                                put("ID", "v02269g10002cc7lo84o553oc24jm250");
                                put("Type", "video");
                                put("TargetTime", new ArrayList<Integer>() {
                                    {
                                        add(0);
                                        add(5000);
                                    }
                                });
                                put("Extra", new ArrayList<HashMap<String,Object>>(){
                                    {
                                        add(new HashMap<String,Object>(){
                                            {
                                                put("ID", "1");
                                                put("Type","transform");
                                                put("PosX",0);
                                                put("PosY",0); //视频充满画面传（0,0)即可
                                                put("Width",1280);
                                                put("Height",720);
                                                // put("Alpha",1);
                                            }
                                        });
                                        add(new HashMap<String,Object>(){
                                            {
                                                put("ID","2");
                                                put("Type","trim");
                                                put("StartTime",0);
                                                put("EndTime",5000);
                                            }
                                        });
                                    }

                                });
                                put("Source", "https://dev.hs.hokkj.cn/1410710a7d7849699d2e610e2a75c562?auth_key=1662005538-d6ad57a1677a4450b98ccb23497e6672-0-d55a94ad644d7dc530fe0b5d79082dda");
                            }
                        });
                    }
                });

            }
        });
        params.put("Canvas",new HashMap<String,Object>(){
            {
                put("Width",1280);
                put("Height",720);
//                put("BackgroundColor","#00000000");
            }
        });
        String paramsJsonString = JSON.toJSONString(params);

        VodSubmitDirectEditTaskAsyncRequest vodSubmitDirectEditTaskAsyncRequest = VodSubmitDirectEditTaskAsyncRequest.newBuilder()
                .setUploader("hok")
                .setApplication("VideoTrackToB")
                .setEditParam(ByteString.copyFromUtf8(paramsJsonString))
                .build();


        try {
            VodSubmitDirectEditTaskAsyncResponse vodSubmitDirectEditTaskAsyncResponse = vodService.submitDirectEditTaskAsync(vodSubmitDirectEditTaskAsyncRequest);
            if (vodSubmitDirectEditTaskAsyncResponse.getResponseMetadata().hasError()) {
                System.out.println(vodSubmitDirectEditTaskAsyncResponse.getResponseMetadata().getError());
                System.exit(-1);
            }
            System.out.println(vodSubmitDirectEditTaskAsyncResponse.toString()); // 编码采用UTF8
            System.out.println(vodSubmitDirectEditTaskAsyncResponse.getResponseMetadata().getRequestId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

